using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CSW.Framework.Common;
using CSW.Framework.Common.Binding.Interfaces;
using DevExpress.XtraEditors;

namespace CSW.Framework.UI.DevEx.Forms
{
    /// <summary>
    /// Multi-select form.
    /// </summary>
    public partial class DXMultiSelectForm : XtraForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DXMultiSelectForm"/> class.
        /// </summary>
        /// <param name="dataList">The data list.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="fields">The fields.</param>
        public DXMultiSelectForm(IDataSelectionList dataList, IList<string> headers, IList<string> fields)
            : this(dataList, headers, fields, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DXMultiSelectForm"/> class.
        /// </summary>
        /// <param name="dataList">The data list.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="isReadOnly">if set to <c>true</c>, the list is made read only.</param>
        public DXMultiSelectForm(IDataSelectionList dataList, IList<string> headers, IList<string> fields, bool isReadOnly)
        {
            Guard.ArgumentNotNull(dataList, "dataList");
            Guard.ArgumentNotNullOrEmpty(headers, "headers");
            Guard.ArgumentNotNullOrEmpty(fields, "fields");

            InitializeComponent();

            multiSelectUserControl1.Setup(dataList, headers, fields, isReadOnly);

            if (isReadOnly)
            {
                btnOK.Visible = false;
                btnCancel.Text = "&OK";
                AcceptButton = btnCancel;
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (!multiSelectUserControl1.IsReadOnly)
            {
                multiSelectUserControl1.DataList.AcceptChanges();
                DialogResult = DialogResult.OK;
            }

            Close();
        }

        private void MultiSelectForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (DialogResult != DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                multiSelectUserControl1.DataList.RevertToSaved();
                Cursor.Current = Cursors.Default;
            }
        }
    }
}